package kgame5.k3d.core.obj3d.node3d.group3d {
import kgame5.k3d.core.obj3d.info.material.MaterialInfo;
import kgame5.k3d.core.obj3d.node3d.animator.Animator3D;
import kgame5.k3d.core.parser.MD2Parser;
import kgame5.kutil.KEvent;
import kgame5.kutil.T;

import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.utils.ByteArray;

/**
 * @author kangkai
 * 注意这个的128行我对mi的cullType的设置，这个主要是为了照顾3dmax里导出的md2
 * 
 * 
 */
public class Loader3D extends BestGroup3D{
	
	public static const 
		EVENTTYPE_LOAD_OK:String="EVENTTYPE_LOAD_OK";
	
	private var sFileUrl:String;
	private var sActionTableUrl:String;
	private var sSkinUrl:String;
	
	private var animator:Animator3D;
		
	public function Loader3D(zbufferWidth:int=-1,zbufferHeight:int=-1){//sortType=1是ccz_bornID
		super(zbufferWidth,zbufferHeight);
	}
	
	public function Load(p_sFileUrl:String,p_sActionTableUrl:String,p_sSkinUrl:String):void{
		
		//提取文件信息
		sFileUrl=p_sFileUrl;
		sActionTableUrl=p_sActionTableUrl;
		sSkinUrl=p_sSkinUrl;
		
		//释放老模型，保证只有一个模型
		this.removeAllChild();
		
		//加载模型
		loadShape();
	}
	//加载模型
	private function loadShape():void{
		var l:URLLoader=new URLLoader();
		l.addEventListener(Event.COMPLETE,onLoadShapeOK);
		
		l.dataFormat=URLLoaderDataFormat.BINARY;
		
		try{
			l.load(new URLRequest(sFileUrl));
		}catch(er:Error){
			T.p("不能加载，发生错误");
		}
	}
	//模型加载成功
	private function onLoadShapeOK(e:Event):void{
		//T.p(this,"onComplete");
		var l:URLLoader=e.currentTarget as URLLoader;
	
		var md2Parser:MD2Parser=new MD2Parser(4);
		md2Parser.parse(l.data as ByteArray);
			
		animator=md2Parser.animator;
			
		//加载动作表
		if(this.sActionTableUrl!=null){//有动作表
			loadActionTable();
		}else{//无动作表
			animator.setupActionTableXML(null);
			
			//加载皮肤
			loadSkin();
		}
	}
	
	//加载动作表
	private function loadActionTable():void{
		var l:URLLoader=new URLLoader();
		l.addEventListener(Event.COMPLETE,onLoadActionTableOK);
		
		l.dataFormat=URLLoaderDataFormat.TEXT;
		
		try{
			l.load(new URLRequest(sActionTableUrl));
		}catch(er:Error){
			T.p("不能加载，发生错误");
		}
	}
	//加载动作表成功
	private function onLoadActionTableOK(e:Event):void{
		//T.p(this,"onComplete");
		var l:URLLoader=e.currentTarget as URLLoader;
	
		animator.setupActionTableXML(new XML(l.data));
			
		//加载皮肤
		loadSkin();
	}
	
	//加载贴图
	private function loadSkin():void{
		//------------加载到了模型之后要开始加载皮肤
		var l:Loader = new Loader();
		
		l.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadSkinOK);
		
		l.load(new URLRequest(sSkinUrl));
	}
	//贴图加载成功
	private function onLoadSkinOK(e:Event):void{
		//T.p("加载皮肤到了，全替换皮肤");
		var l : Loader = LoaderInfo(e.target).loader;
		var bmd : BitmapData = Bitmap(l.content).bitmapData;
		
		this.ti.bForceRefresh=true;//这个会通知更新整个模型
		
		var mi:MaterialInfo=new MaterialInfo();
		mi.bmd=bmd;
			
		//严重注意这个我在这里是做了翻转的
		//mi.cullType=MaterialInfo.CULLTYPE_NONE;
		animator.setMaterialInfo(mi);
		
		this.addChild(animator);
		
		//-----发出说自己加载完毕
		this.dispatchEvent(new KEvent(EVENTTYPE_LOAD_OK));
	}
	
}//end of class
}
